const Router = require('koa-router')

const {success} = require('../../lib/helper')

const {
    Auth
} = require('../../../middlewares/auth')

const {
    MainContentValidator
} = require('../../validators/validator')

const {Booking} = require("../../models/booking");
const {Equipment} = require("../../models/equipment");


const router = new Router({
    prefix: '/v1/booking'
})


/**
 * 用户上传附件的接口 （基本信息+摘要文章）
 * id    主键
 * uid   用户（申请人）id
 * mid   设备id
 * start    开始时间（预约）
 * end      结束时间（预约）
 * status   预约状态 0待审批  1预约成功  2拒绝预约
 */
router.post('/add', new Auth().m, async (ctx) => {
    const v = await new MainContentValidator().validate(ctx)
    const booking = {
        uid: v.get('body.uid'),
        mid: v.get('body.mid'),
        start: v.get('body.start'),
        end: v.get('body.end'),
        status: v.get('body.status'),
    }

    await Booking.create(booking)
    success()
})


/*
*
*
* */
router.post('/listHis', async (ctx) => {
    const v = await new MainContentValidator().validate(ctx);
    const mid = v.get('body.mid');
    const status = v.get('body.status');
    let data = await Booking.getBookingHisList(mid, status);
    success(data);
})


/*
* 分页查询
*
* */
// router.get('/list', async ctx => {
//     const v = await new MainContentValidator().validate(ctx);
//     // 从查询字符串中获取分页参数
//     console.log('ctx====    ',ctx)
//     const {status = '', page = 1, pagesize = 10} = ctx.query;
//     let data = await Booking.getBookingDataWithPagination(status, page, pagesize)
//     success(data);
// })

router.get('/list', async ctx => {
    const v = await new MainContentValidator().validate(ctx);
    const {status = '', page = 1, pagesize = 10} = ctx.query;
    let data = await Booking.getBookingDataWithPagination(status, page, pagesize);
    success(data);
})



// router.get('/list', async ctx => {
//     const v = await new MainContentValidator().validate(ctx);
//
//     // 使用参数化查询替代直接拼接
//     const status = `'${ctx.query.status}'` || ''; // 转义状态字符串
//     const page = parseInt(ctx.query.page, 10) || 1; // 转义并转换页码为整数
//     const pageSize = parseInt(ctx.query.pagesize, 10) || 10; // 转义并转换页面大小为整数
//
//     let data = await Booking.getBookingDataWithPagination(status, page, pageSize);
//     success(data);
// })




// router.post('/listUser', async (ctx) => {
//     const v = await new MainContentValidator().validate(ctx);
//     // 从查询字符串中获取分页参数
//     let page = v.get('body.page');//设备id
//     let pagesize = v.get('body.pagesize');//查询参数  开始时间
//     let uid = v.get('body.uid')
//     console.log('uid=  ', uid)
//     let data = await Booking.getUserBookingDataWithPagination(page, pagesize, uid)
//     success(data);
// })

router.post('/listUser', async (ctx) => {
    const v = await new MainContentValidator().validate(ctx);
    // 从查询字符串中获取分页参数
    let page = parseInt(v.get('body.page')) || 1; // 设备id
    let pagesize = parseInt(v.get('body.pagesize')) || 10; // 查询参数  开始时间
    let uid = v.get('body.uid'); // 用户ID

    // 确保uid是有效的数字，防止SQL注入
    if (isNaN(uid) || parseInt(uid) <= 0) {
        ctx.status = 400;
        ctx.body = { error: 'Invalid uid parameter' };
        return;
    }

    // 使用参数化查询进行分页查询
    let data = await Booking.getUserBookingDataWithPagination(page, pagesize, parseInt(uid));
    success(data);
})




router.post('/update', async (ctx) => {
    const v = await new MainContentValidator().validate(ctx)

    let id = v.get('body.id')
    let uid = v.get('body.uid')
    let mid = v.get('body.mid')
    let start = v.get('body.start')
    let end = v.get('body.end')
    let status = v.get('body.status')


    await Booking.updateMoudle(id, uid, mid, start, end, status)
    success()
})

router.post('/pass', async (ctx) => {
    const v = await new MainContentValidator().validate(ctx)

    let id = v.get('body.id')
    let status = v.get('body.status')
    let memo = v.get('body.memo')

    // 更新预约状态
    await Booking.updateStatus(id, status, memo)

    success()
})


router.post('/del', async (ctx) => {
    const v = await new MainContentValidator().validate(ctx);
    const id = v.get('body.id');
    console.log('id========', id)
    await Booking.deleteMoudle(id);
    success();
})


router.post('/checkTimeRange', async (ctx) => {
    const v = await new MainContentValidator().validate(ctx);
    const mid = v.get('body.mid');//设备id
    const stime = v.get('body.stime');//查询参数  开始时间
    const etime = v.get('body.etime');//查询参数  结束时间

    let result = await Booking.getRangeBoolean(mid, stime, etime)
    console.log('getRangeBoolean= ', result)
    success(result);
})


module.exports = router
